package com.luocj.muke.demo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luocj.muke.demo.activity.BaseActivity;
import com.luocj.muke.demo.fragment.HomeFragment;
import com.luocj.muke.demo.fragment.MineFragment;
import com.luocj.muke.demo.fragment.NewsFragment;
import com.luocj.muke.demo.fragment.ReviewFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();
    private FrameLayout container;
    private RelativeLayout home;
    private RelativeLayout news;
    private RelativeLayout mine;
    private RelativeLayout review;
    private ImageView homeImageview;
    private ImageView newsImageview;
    private ImageView mineImageview;
    private ImageView reviewImageview;
    private TextView homeTextview;
    private TextView newsTextview;
    private TextView mineTextview;
    private HomeFragment homeFragment;
    private NewsFragment newsFragment;
    private MineFragment mineFragment;
    private ReviewFragment reviewFragment;
    private android.support.v4.app.FragmentManager fm;
    private Fragment mCurrentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        container = findViewById(R.id.fl_contaner);
        home = findViewById(R.id.rl_home);
        news = findViewById(R.id.rl_news);
        mine = findViewById(R.id.rl_mine);
        review = findViewById(R.id.rl_review);

        homeImageview = findViewById(R.id.iv_home);
        newsImageview = findViewById(R.id.iv_news);
        mineImageview = findViewById(R.id.iv_mine);
        reviewImageview = findViewById(R.id.iv_review);
        homeTextview = findViewById(R.id.tv_home);
        newsTextview = findViewById(R.id.tv_news);
        mineTextview = findViewById(R.id.tv_mine);
        homeImageview.setImageResource(R.mipmap.comui_tab_home_selected);
        newsImageview.setImageResource(R.mipmap.comui_tab_message);
        mineImageview.setImageResource(R.mipmap.comui_tab_person);
        reviewImageview.setImageResource(R.mipmap.comui_tab_person);

        home.setOnClickListener(this);
        news.setOnClickListener(this);
        mine.setOnClickListener(this);
        review.setOnClickListener(this);

        homeFragment = new HomeFragment();
        fm = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fl_contaner, homeFragment);
        fragmentTransaction.commit();

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        switch (id) {
            case R.id.rl_home:
                Log.i(TAG, "onClick: ");
                homeImageview.setImageResource(R.mipmap.comui_tab_home_selected);
                newsImageview.setImageResource(R.mipmap.comui_tab_message);
                mineImageview.setImageResource(R.mipmap.comui_tab_person);
                hideFragment(newsFragment, fragmentTransaction);
                hideFragment(mineFragment, fragmentTransaction);
                hideFragment(reviewFragment, fragmentTransaction);
                if (null == homeFragment) {
                    homeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.fl_contaner, homeFragment);
                } else {
                    mCurrentFragment = homeFragment;
                    fragmentTransaction.show(mCurrentFragment);
                }
                break;
            case R.id.rl_news:
                Log.i(TAG, "onClick: ");
                homeImageview.setImageResource(R.mipmap.comui_tab_home);
                newsImageview.setImageResource(R.mipmap.comui_tab_message_selected);
                mineImageview.setImageResource(R.mipmap.comui_tab_person);
                hideFragment(homeFragment, fragmentTransaction);
                hideFragment(mineFragment, fragmentTransaction);
                hideFragment(reviewFragment, fragmentTransaction);
                if (null == newsFragment) {
                    newsFragment = new NewsFragment();
                    fragmentTransaction.add(R.id.fl_contaner, newsFragment);
                } else {
                    mCurrentFragment = newsFragment;
                    fragmentTransaction.show(mCurrentFragment);
                }
                break;
            case R.id.rl_mine:
                Log.i(TAG, "onClick: ");
                homeImageview.setImageResource(R.mipmap.comui_tab_home);
                newsImageview.setImageResource(R.mipmap.comui_tab_message);
                mineImageview.setImageResource(R.mipmap.comui_tab_person_selected);
                hideFragment(homeFragment, fragmentTransaction);
                hideFragment(newsFragment, fragmentTransaction);
                hideFragment(reviewFragment, fragmentTransaction);
                if (null == mineFragment) {
                    mineFragment = new MineFragment();
                    fragmentTransaction.add(R.id.fl_contaner, mineFragment);
                } else {
                    mCurrentFragment = mineFragment;

                    fragmentTransaction.show(mCurrentFragment);
                }
                break;

            case R.id.rl_review:
                Log.i(TAG, "onClick: ");
                homeImageview.setImageResource(R.mipmap.comui_tab_home);
                newsImageview.setImageResource(R.mipmap.comui_tab_message);
                mineImageview.setImageResource(R.mipmap.comui_tab_person);
                hideFragment(homeFragment, fragmentTransaction);
                hideFragment(newsFragment, fragmentTransaction);
                hideFragment(mineFragment, fragmentTransaction);

                if (null == reviewFragment) {
                    reviewFragment = new ReviewFragment();
                    fragmentTransaction.add(R.id.fl_contaner, reviewFragment);
                } else {
                    mCurrentFragment = reviewFragment;

                    fragmentTransaction.show(mCurrentFragment);
                }
                break;

        }

        fragmentTransaction.commit();
    }

    private void hideFragment(Fragment fragment, FragmentTransaction fragmentTransaction) {
        if (null != fragment)
            fragmentTransaction.hide(fragment);
    }
}
